<?php

namespace App\Admin\Controllers\Stat;

use App\Http\Controllers\Controller;
use App\Model\AppChannel;
use App\Model\Stat\StatDaily;
use App\Model\Stat\StatDailyCall;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class StatDailyCallController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('话单日报');
        $content->body($this->grid());
        return $content;
    }

    /**
     * $table->tinyInteger('call_type')->default(-1)->comment('话单类型');
        $table->integer('call_num')->default(0)->comment('总话单');
        $table->integer('success_num')->default(0)->comment('成功话单');
        $table->integer('caller_num')->default(0)->comment('总呼叫人数');
        $table->integer('receiver_num')->default(0)->comment('总被叫人数');
        $table->integer('success_caller_num')->default(0)->comment('成功拨打人数');
        $table->integer('success_receiver_num')->default(0)->comment('成功接听人数');
        $table->integer('total_call_time')->default(0)->comment('话单总时长');
        $table->integer('call_num_fail')->default(0)->comment('失败话单数');
        $table->integer('call_num_noanswer')->default(0)->comment('无人接听话单数');
        $table->integer('call_num_hangup')->default(0)->comment('主叫挂断');
        $table->integer('call_num_reject')->default(0)->comment('被叫拒绝');
        $table->integer('call_num_err')->default(0)->comment('系统异常挂断');
     */
    protected function grid()
    {
        $grid = new Grid(new StatDailyCall());
        $grid->model()->where(['channel' => ''])->orderBy('dt', 'desc');
        $grid->dt('日期');
        $grid->call_type('话单类型')->display(function ($data) {
            return $data == VoiceCall::CALL_TYPE ? '语音' : '视频';
        });
        $grid->call_subtype('话单子分类')->display(function ($data) {
            return StatDailyCall::SUB_TYPE[$data] ?? '-';
        });
        $grid->call_num('话单总数');
        $grid->success_num('成功话单数');
        $grid->success_percent('接通率')->display(function ($data) {
            if (!$this->call_num) return '-';
            return number_format($this->success_num / $this->call_num, 2) * 100 . "%";
        });
        $grid->caller_num('总呼叫人数');
        $grid->receiver_num('总被叫人数');
        $grid->success_caller_num('成功拨打人数');
        $grid->success_receiver_num('成功接听人数');
        // $grid->total_call_time('话单总时长（秒）');
        $grid->total_call_minutes('话单总时长（分钟）');
        $grid->average_call_minutes('平均通话时长（分钟）')->display(function ($data) {
            if (!$this->success_num) return '-';
            return round($this->total_call_minutes / $this->success_num, 1);
        });
        $grid->call_num_fail('失败话单数');
        $grid->call_num_noanswer('无人接听话单数');
        $grid->call_num_hangup('主叫挂断数');
        $grid->call_num_reject('被叫拒绝数');
        $grid->call_num_err('系统异常挂断数');
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('call_type', '话单类型')->select([VoiceCall::CALL_TYPE => '语音', VideoCall::CALL_TYPE => '视频']);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('call_subtype', '话单子类')->select(StatDailyCall::SUB_TYPE);
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->date('dt', '日期');
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }

    function getChannels()
    {
        $list = AppChannel::where(['flag' => 1])->select(['channel_name', 'channel'])->get()->toArray();
        $data = [['id' => 'all', 'text' => '全部渠道']]; //
        foreach ($list as $item) {
            $data[] = ['id' => $item['channel'], 'text' => $item['channel_name']];
        }
        return $data;
    }
}
